import gtk
import os.path
import mlib_utils
import xmmsclient
import xmmsclient.collections
from xmmsclient.collections import *


class BroadcastManager:
    iters_pool = []
    
    def __init__(self, mlib, xmms2):
        treeview = mlib.get_widget("treeview1")
        model = treeview.get_model()
        treeview.set_row_separator_func(self.is_separator)
        self.mlib = mlib
        self.model = model
        self.treeview = treeview
        self.xmms2 = xmms2
        
        xmms2.coll_query_infos(
            Intersection(
                Universe(), 
                Has(Universe(), field="artist"),
                Has(Universe(), field="album"), 
                Has(Universe(), field="title")), 
            ("artist", "album", "title", "picture_front"), 
            leng=0,
            order=None,
            groupby=["artist", "album", "title"],
            cb=self.insert_entries)

    def insert_entries(self, res):
        if res.iserror():
            print "[BroadcastManager] Could not insert entries"
            return
        all_entries = res.value()
        current_artist = None
        current_album = None
        entry = all_entries.pop(0)
        artist = entry["artist"]
        album = entry["album"]
        title = entry["title"]
        hash = entry["picture_front"]
        current_artist = self.model.append(
            None, 
            [None, "artist", artist, -1, False, "#B1C0E1"])
        current_album = self.model.append(
            current_artist, 
            [None, "album", album, -1, False, None])
        self.model.append(
            current_album, 
            [None, "title", title, -1, False, None])
        if hash is not None:
            self.iters_pool.append(current_album)
            self.xmms2.bindata_retrieve(hash, cb=self.add_cover)
        for e in all_entries:
            artist = e["artist"]
            album = e["album"]
            title = e["title"]
            hash = e["picture_front"]
            if not artist == self.model.get_value(current_artist, mlib_utils.VALUE):
                self.model.append(None, [None, None, None, -1, True, None])
                current_artist = self.model.append(
                    None, 
                    [None, "artist", artist, -1, False, "#B1C0E1"])
            if not album == self.model.get_value(current_album, mlib_utils.VALUE):
                current_album = self.model.append(
                    current_artist,
                    [None, "album", album, -1, False, None])
                if hash is not None:
                    self.iters_pool.append(current_album)
                    self.xmms2.bindata_retrieve(hash, cb=self.add_cover)
            self.model.append(current_album, [None, "title", title, -1, False, None])
            
        self.treeview.expand_all()

    def is_separator(self, model, iter):
        is_sep = model.get_value(iter, mlib_utils.SEPARATOR)
        return is_sep

    def add_cover(self, res):
        if res.iserror():
            print "[BroadcastManager] Could not insert entries"
            self.iters_pool.pop(0)
            return
        iter = self.iters_pool.pop(0)
        cover = res.value()
        loader = gtk.gdk.PixbufLoader()
        loader.set_size(mlib_utils.THUMBNAILS_SIZE, 
        mlib_utils.THUMBNAILS_SIZE)
        loader.write(cover)
        loader.close()
        pb = loader.get_pixbuf()
        self.model.set(iter, 0, pb)
